package com.android.monee.data.access.inMemory;

import java.util.ArrayList;
import java.util.List;

import com.android.monee.data.access.DaoListener;
import com.android.monee.data.access.IdNClone;

public abstract class AbstractObservableDaoInMemory<T extends IdNClone<I, T>, I> extends AbstractDaoInMemory<T, I> implements ObservableDao<T, I>{

	private List<DaoListener<T, I>> listeners;
	
	public AbstractObservableDaoInMemory(){
		this.listeners = new ArrayList<DaoListener<T,I>>();
	}
	
	@Override
	public void insert(T obj) {
		super.insert(obj);
		for(DaoListener<T, I> listener : listeners){
			listener.onInsert(obj);
		}		
	}

	abstract protected I getNextId();
	
	@Override
	public void update(T obj) {
		super.update(obj);
		for(DaoListener<T, I> listener : listeners){
			listener.onUpdate(obj);
		}
	}

	@Override
	public void delete(I id) {
		super.delete(id);
		for(DaoListener<T, I> listener : listeners){
			listener.onDelete(id);
		}
	}

	@Override
	public void addDaoListener(DaoListener<T, I> listener) {
		listeners.add(listener);		
	}

	@Override
	public void deleteDaoListener(DaoListener<T, I> listener) {
		listeners.remove(listener);
	}
}
